#include <stdio.h>
#include <string.h>
#include "port.h"
#include "dvb.h"
#include "sipsiparser.h"
#include "secdb.h"
#include "dvbgroupdb.h"
#include "dvbtsdb.h"
#include "dvbservicesdb.h"
#include "dvbuserdb.h"
#include "builderframework.h"
#include "dvbtdt.h"
#include "dec_api.h"

#ifndef DEFINED_BOOL
#define DEFINED_BOOL
typedef int BOOL;
#endif

#define		TRUE	1
#define		FALSE	0

/************************************************************************/
/*                            DVB SI STRUCTURE                          */
/************************************************************************/
#define SIACTURAL 						1
#define SIOTHER  						2	
#define SIBOTH							3

#define MAX_TS_NUM 						100

//nvm si data section max len, last 4 bytes is crc
#define NVM_SIDATA_MAX_LEN 			0x20000 


#define MIN(a,b)	((a) < (b) ? (a) : (b))

#define VERIFIED_BYTES 					0x82101215

typedef enum
{
	SERVICE_LOCK_FAIL,
	SERVICE_PLAY_FAIL,
}SERVICE_PLAY_REASON_T;

/************************************************************************/
/*					  CURRENT PLAY INFO STRUCTURE                       */
/************************************************************************/

#define SI_MAX_ECM_NUM					4
typedef struct 
{
	unsigned short	CAID;
	unsigned short	PID;
}ECMINFO,EMMINFO;

typedef struct 
{
	unsigned char		type;
	unsigned char		ComponentTag;	
	unsigned short		pid;
	unsigned long		language;//the first three byte is the language code, the last byte is 0
	unsigned long		language1;//the first three byte is the language code, the last byte is 0,when the pid is data_broadcast, the first byte is the component_tag
	short				ecmNum;
	ECMINFO				ecmInfo[SI_MAX_ECM_NUM];
}PIDINFO;

extern int	newChannel;

extern char sys_lan[4];

typedef void (*pServicePlayNotify)(int, dvb_service_t*);

typedef struct 
{
	dvb_tuner_t tunerInfo;
	dvb_service_t handle;
	unsigned short vPid;
	unsigned short vType;
	unsigned short aPid;
	unsigned short aType;
	unsigned short pPid;
	unsigned int	vol;
	unsigned int	stero;
	pServicePlayNotify callback;
	unsigned short pmtpid;
	unsigned short pmtlen;
	unsigned char pmt[1024];
}play_control_param_t;

typedef struct
{
	unsigned char	Language[4];
	unsigned short	subtitling_type;
  	unsigned short	composition_page_id;		
  	unsigned short	ancillary_page_id;										
}SUBTITLING_DESCRIPTOR_INFO;

typedef struct
{
	unsigned short			pid;
	int						DescCount;		
	SUBTITLING_DESCRIPTOR_INFO	SutTitleDesc[10];
}SUBTITLEINFO;

typedef struct
{
	char szLangCode[10];
	unsigned short pid;
}AUDIOLANGINFO;


typedef void (*pDVBInitCallback)(int status);

extern int DvbRecvNIT(int Param);
extern int DvbSecFinishCheck(unsigned char* pu8RecvBitmap, unsigned char u8LastSecNo, unsigned char u8CurSecNo);
extern int DvbSecRecvCheck(unsigned char* pu8RecvBitmap, unsigned char u8CurSecNo);

extern int DvbMonitorTunerInit();

 extern dvb_time_t GetEndTime(dvb_time_t  time,int duration);
  
extern void printHex(unsigned char *buf,int len);
extern int LoadCurService();
extern int SaveCurService();
extern dvb_service_t DvbGetCurService();
dvb_service_t DvbGetLastService();

extern unsigned int GetCurFrequency(void);
extern int DvbSiLoadData();
extern int DvbSiSaveData();

extern int DvbPlayNewChannel(play_control_param_t* pstChannel);
extern int DvbPlayStop();
extern int DvbPlayTaskInit();

extern int DvbSetChannelPlayFlag(int flag);
extern int DvbGetChannelPlayFlag();

extern dvb_time_t DVBGetSystemTime();

typedef enum
{
	PAT_VERSION_CHANGE,
	PMT_VERSION_CHANGE,
	SDT_VERSION_CHANGE,
	BAT_VERSION_CHANGE,
	NIT_VERSION_CHANGE,
}VERSION_CHANGE;
typedef void (*pSIChangeNotifyFun)(unsigned char, unsigned char*);


typedef void (*pEitCallback)(void *);
typedef void (*pSignalBadNotify)(int bad);


//service api
extern int DVBIsServiceEncrypt(dvb_service_t* handle);
extern int DVBServiceGetAudioLangInfo(dvb_service_t* handle, AUDIOLANGINFO* pAudioInfo, unsigned int* pNum);
extern int DVBServiceGetSubtitleInfo(dvb_service_t* handle,SUBTITLEINFO *pSubtitleInfo);
extern int DVBServiceGetTeletextInfo(dvb_service_t* handle, sipsi_teletextinfo_t *pInfo, unsigned int* pNum);
extern int DVBServicesDBSetVolume(dvb_service_t* SerHandle,int vol);
extern int DVBServicesDBGetVolume(dvb_service_t* SerHandle);
extern int DVBServicesDBSetStereoMode(dvb_service_t* SerHandle,int mode);
extern int DVBServicesDBGetStereoMode(dvb_service_t* SerHandle);
extern int DVBTSGetTunerInfoByServiceHandle(dvb_service_t* SerHandle, dvb_tuner_t* pstTunerinfo);
extern dvb_service_t DvbGetCurService();
extern int DvbSetCurService(dvb_service_t* handle);
extern dvb_service_t DvbGetLastService();
extern int DvbLastServiceInvalidate();
extern int DvbServiceInvalidate();
extern int DvbCheckServiceHandle(dvb_service_t* handle);
extern int DVBServicePlay(int hPlay,dvb_service_t* handle, pServicePlayNotify callback);
extern int DVBServicePlayNext(int hPlay,pServicePlayNotify callback);
extern int DVBServicePlayPrevious(int hPlay,pServicePlayNotify callback);
extern int DVBServicePlayByIndex(int hPlay,unsigned short index, pServicePlayNotify callback);
extern int DVBServicePlayLast(int hPlay,pServicePlayNotify callback);
extern int DVBServicePlayStop(int hPlay);
extern int DVBServiceSetAudioIndex(dvb_service_t* handle, unsigned int index);
extern int DVBServicesDBGetAudioIndex(dvb_service_t* handle);
extern int DVBServicesDBGetSubLangIndex(dvb_service_t* handle);
extern int DVBServicesDBSetSubLangIndex(dvb_service_t* handle, int index);
extern int DVBServicesDBReName(dvb_service_t* handle,char* pSzName,int len);
extern int DVBServicesDBSetLock(dvb_service_t* SerHandle,int bLock);
extern int DVBServicesDBGetLock(dvb_service_t* SerHandle);
extern int DVBServiceGetIndexByTsidAndServiceid(unsigned short network_id, unsigned short ts_id, unsigned short service_id);

//search api
extern int DvbBuilderSetFrequencyTable(dvb_frequency_t* pTable, unsigned int num);
extern int DVBSetInSourceType(int type);
extern int DVBTunerLock(dvb_tuner_t* params);
extern int DVBTunerForceLock(dvb_tuner_t* params);
extern int  DVBGetSignalStatus(dvb_tuner_status_t *status);
extern int DvbRegisterSignalBadNotify(pSignalBadNotify callback);
//SYSTEM api
extern dvb_time_t DVBGetSystemTime();
extern int DVBSetSystemTimezone (char timezone);
extern int DVBGetSystemTimezone ();
extern pSIChangeNotifyFun DvbRegisterSIChangeNotify(pSIChangeNotifyFun callback);
extern int DVBSetSystemLanguage (char* lan);//add by jjzhao to set system language
extern int DvbSystemSetAudioMode(int mode);
extern int DvbSystemGetAudioMode();
extern int DVBRestoreFactorySetting();
extern int DvbResetServicesList();
extern int DVBSystemInitialize();
extern int DVBSystemDeinitialize();
extern int DVBSystemSetFreqParams(dvb_tuner_t* pFreqParams);
extern int DVBSystemGetFreqParams(dvb_tuner_t* pFreqParams);

extern int DVBNetWorkUpdateProgramStart();
extern int DVBNetWorkUpdateProgramStop();
extern int DVBNetWorkUpdateProgram(dvb_networt_service_t* pstProgramList, unsigned int u32Num, unsigned int u32GroupIndex, const char* ps8GroupName);

extern int DVBTunerStandby();
extern int DVBTunerResume();
extern int DVBTunerReset();

extern int DVBGetMagicId();

extern void DVBServiceDescrambling();
extern int DVBServiceDescrambleCheckByIndex(unsigned int u32Index);

extern int DVBSetDemoParam(dvb_demo_param_t* pstParam, unsigned int u32Num);

extern int DVBRegisterDvbInitDoneNotify(pDVBInitCallback callback);


